package java课题设计.功能类;
import java课题设计.工具类.RandomID;

import javax.print.Doc;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
 
//该类用于登陆之选择添加学生到数据库中的操作页面绘制以及代码逻辑
public class AddStudent extends JFrame implements MouseListener, DocumentListener {
    private JFrame pre;
    JTextField nametext=new JTextField();
    JTextField birthname=new JTextField();
    JButton boy=new JButton("男");
    JButton gril=new JButton("女");
    JButton sure = new JButton("确认添加");
    String stuname;
    String sex;
    String birthDate;
    String s;
    public AddStudent(JFrame pre){
        extracted();
        iniImage();
 
        this.pre=pre;
        this.setVisible(true);
    }
    public void extracted(){
        this.setSize(700,500);//设置窗口大小
        this.setTitle("添加学生信息");//设置窗口标题
        this.setAlwaysOnTop(true);//设置此窗口总是位于其他窗口之上
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭方式，3为关闭当前窗口则结束程序运行
        this.setLayout(null);//设置布局模式，null模式表示不受管理器设置管理，所有窗口的显示都由x,y显示
        this.setLocationRelativeTo(null);//设置窗口显示在中间位置
 
        //添加监听事件
        nametext.addMouseListener(this);
        birthname.addMouseListener(this);
        sure.addMouseListener(this);
        gril.addMouseListener(this);
        boy.addMouseListener(this);
    }
    public void iniImage(){
        //添加学号输入提示
        JLabel id=new JLabel("学号");
        id.setBounds(180,75,300,45);
        Font f=new Font("宋体",Font.BOLD,30);
        id.setFont(f);
        this.getContentPane().add(id);
 
        //添加学号输入框
        JTextField idtext=new JTextField();
        idtext.setBounds(280,80,250,35);
        this.getContentPane().add(idtext);
        //随机生成学号并且记录
        RandomID randomID=new RandomID();
        int sid= randomID.getid();
        s=new String(String.valueOf(sid));
        JTextField stuid=new JTextField(s);
        stuid.setBounds(280,80,250,35);
        stuid.setFont(f);
        this.getContentPane().add(stuid);
 
 
        //添加学生姓名输入提示
        JLabel name=new JLabel("姓名");
        name.setBounds(180,150,300,45);
        name.setFont(f);
        this.getContentPane().add(name);
 
        //添加姓名输入框
 
        nametext.setBounds(280,155,250,35);
        this.getContentPane().add(nametext);
        //获取到用户输入的姓名
        nametext.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                stuname=nametext.getText();
            }
        });
 
 
        //添加性别输入提示
        JLabel sex=new JLabel("性别");
        sex.setBounds(180,225,300,45);
        sex.setFont(f);
        this.getContentPane().add(sex);
 
        //添加性别按钮选择
        boy.setBounds(280,225,70,45);
        boy.setFont(f);
        this.getContentPane().add(boy);
        gril.setBounds(380,225,70,45);
        gril.setFont(f);
        this.getContentPane().add(gril);
 
 
 
 
        //添加出生日期输入提示
        JLabel birth=new JLabel("出生日期");
        birth.setBounds(130,305,300,45);
        birth.setFont(f);
        this.getContentPane().add(birth);
 
        //添加出生日期输入框
        birthname.setBounds(280,310,250,35);
        this.getContentPane().add(birthname);
        //添加监听器获取用户输入的出生日期
        birthname.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                birthDate = birthname.getText();
                //System.out.println("输入了出生日期");
            }
 
            @Override
            public void removeUpdate(DocumentEvent e) {
                birthDate = birthname.getText();
                //System.out.println("输入了出生日期");
            }
 
            @Override
            public void changedUpdate(DocumentEvent e) {
                birthDate = birthname.getText();
                //System.out.println("输入了出生日期");
            }
        });
        //添加确定添加学生按钮
        sure.setFont(f);
        sure.setBounds(250,370,180,45);
        this.getContentPane().add(sure);
    }
    public void write() throws IOException {
        char[] b=birthDate.toCharArray();
        char[] c=new char[b.length+3];
        for(int i=0,j=0;i<b.length;){
            if(j==4||j==7||j==10) {c[j]='-';j++;}
            else {c[j]=b[i];j++;i++;}
 
        }
        birthDate=new String(c,0,c.length-1);
        String str=stuname+"\t"+sex+"\t"+s+"\t"+"0\t"+"0\t"+"0\t"+birthDate+"\n";
        FileOutputStream fw=new FileOutputStream("D:\\\\\\\\aaa\\\\\\\\数据\\\\\\\\学生数据.txt",true);
        byte[] byt = str.getBytes();
        fw.write(byt);
        System.out.println("添加成功");
        System.out.println(str);
        fw.close();
    }
 
    @Override
    protected void processWindowEvent(WindowEvent e){
        super.processWindowEvent(e);
        if(e.getID()==WindowEvent.WINDOW_CLOSED){
            this.setVisible(false);//隐藏当前窗口
            pre.setVisible(true);//显示上一级窗口
        }
    }
 
    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj=e.getSource();
        if(obj==sure){
            System.out.println("点击了完成按钮");
            //调用方法写入数据
            try {
                write();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //回到选择操作的窗口
            pre.setVisible(true);
            //关闭当前窗口
            this.setVisible(false);
        }
        else if(obj==gril){
            sex="女";
            System.out.println("性别为女");
        }
        else if(obj==boy){
            System.out.println("性别为男");
            sex="男";
        }
    }
 
    @Override
    public void mousePressed(MouseEvent e) {
 
    }
 
    @Override
    public void mouseReleased(MouseEvent e) {
 
    }
 
    @Override
    public void mouseEntered(MouseEvent e) {
 
    }
 
    @Override
    public void mouseExited(MouseEvent e) {
 
    }
 
    @Override
    public void insertUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void removeUpdate(DocumentEvent e) {
 
    }
 
    @Override
    public void changedUpdate(DocumentEvent e) {
 
    }
}
